package org.ithasu.filpilote.data.entities;

import org.ithasu.tools.data.EList;
import org.ithasu.tools.data.Entity;
import org.ithasu.tools.net.JSONTools;
import org.ithasu.tools.net.JSONTools.JSONContext;
import org.ithasu.tools.net.JSONTools.JSONEntity;
import org.json.simple.JSONObject;

public final class Zone implements JSONEntity, Entity {

	private final static String JSON_PROPERTY_INDEX = "index";
	private final static String JSON_PROPERTY_NAME = "name";
	private final static String JSON_PROPERTY_WIRES = "wires";

	private long index;
	private String name;
	private final EList<Wire> wires;

	public Zone(JSONContext context, JSONObject o) {
		this.wires = new EList<Wire>(null); // Will be initialized by parseJSON
		parseJSON(context, o);
	}

	// -- Entity ----------------------------------------

	@Override
	public long getId() {
		return index;
	}

	// -- Properties ----------------------------------------

	public final String getName() {
		return name;
	}

	public final EList<Wire> getWires() {
		return wires;
	}

	// -- JSON ----------------------------------------

	@SuppressWarnings("unchecked")
	@Override
	public JSONObject toJSON(JSONContext context) {
		JSONObject o = new JSONObject();
		o.put(JSON_PROPERTY_INDEX, index);
		o.put(JSON_PROPERTY_NAME, name);
		o.put(JSON_PROPERTY_WIRES, wires.toJSON(context));
		return o;
	}

	@Override
	public void parseJSON(JSONContext context, JSONObject o) {
		this.index = JSONTools.getProperty(Long.class, o, JSON_PROPERTY_INDEX, -1l);
		this.name = JSONTools.getProperty(String.class, o, JSON_PROPERTY_NAME, "<no name>");
		this.wires.parseJSON(context, JSONTools.getProperty(JSONObject.class, o, JSON_PROPERTY_WIRES));
	}

}
